//
//  LoginViewController.m
//  QBIM
//
//  Created by liu nian on 14-3-18.
//  Copyright (c) 2014年 liunian. All rights reserved.
//

#import "LoginViewController.h"
#import "MessageViewController.h"
#import "MessageCenter.h"
#import "BodyBase.h"
#import "JSON.h"
#import "PacketUtility.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginInfo:) name:@"LOGIN" object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loginInfo:(NSNotification*) aNotification{
    RespBody *respBody = [aNotification object];
    
    if (respBody.EC == 0) {
        dispatch_async(dispatch_get_main_queue(), ^{
            MessageViewController *chatController = [[MessageViewController alloc] init];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:chatController];
            [self presentViewController:nav animated:YES completion:nil];
        });

    }
}

- (IBAction)login:(id)sender{
    
    AuthPayload *ap = [[AuthPayload alloc] init];
    ap.COOKIE = @"ABCDEFGHI";
    ap.ID = [self.nameTextField.text intValue];
    
    [[MessageCenter shared] setAuthPayLoad:ap];
    AuthBody *authParm= [AuthBody parmObj];
    authParm.I = [PacketUtility getPacketSeqID];
    authParm.V = @"v1.0.0";
    authParm.AT = AuthTypeCasToken;
    authParm.AP = [ap.parm2jsonObj JSONFragment];
    
    HeaderBody *body = [[HeaderBody alloc] initWithBody:authParm];
    body.packetType = PacketTypeAuthRequest;
    
    [[MessageCenter shared] auth:body];
}

@end
